package com.gdlgxy.oa.controller;

import com.gdlgxy.oa.bean.Student;
import com.gdlgxy.oa.bean.StudentList;
import com.gdlgxy.oa.service.StudentServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;

/**
 * 网页控制，遍历学生链表，以表格形式输出
 * 该页面连接了"支出"功能
 */
public class StudentListCtrl extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 获取动态应用的根路径
        String contextPath = request.getContextPath();

        // 设置响应的内容类型，防止中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        out.print("<!DOCTYPE html>");
        out.print("<html lang='zh-CN'>");
        out.print("<head>");
        out.print("    <meta charset='UTF-8'>");
        out.print("    <meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.print("    <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.print("    <title>学生列表</title>");
        out.print("    <style>");
        out.print(" h1 {");
        out.print("     padding: 0px 20px;");
        out.print("     border-left: 10px solid #ed515191;");
        out.print("     background-color: rgba(208, 208, 208, 0);");
        out.print("     font-size: 19px;");
        out.print("     line-height: 30px;");
        out.print("     color: cornflowerblue;");
        out.print("     font-weight: bold;");
        out.print("     margin: 5px 0;");
        out.print(" }");
        out.print(" h2 {");
        out.print("     padding: 0px 20px;");
        out.print("     border-left: 10px solid #BF51ED91;");
        out.print("     background-color: rgba(208, 208, 208, 0);");
        out.print("     font-size: 18px;");
        out.print("     line-height: 30px;");
        out.print("     color: cornflowerblue;");
        out.print("     font-weight: bold;");
        out.print("     margin: 5px 0;");
        out.print(" }");
        out.print(" a {");
        out.print("     text-decoration: none;");
        out.print(" }");
        out.print(" div {");
        out.print("     padding: 50px;");
        out.print("     width: 64px;");
        out.print("     margin: auto");
        out.print(" }");
        out.print(" /*表格样式*/");
        out.print(" table {");
        out.print("     width: 90%;");
        out.print("     background: #ccc;");
        out.print("     margin: 10px auto;");
        out.print("     border-collapse: collapse;");
        out.print("     /*border-collapse:collapse合并内外边距(去除表格单元格默认的2个像素内外边距*/");
        out.print(" }");
        out.print(" th,");
        out.print("         td {");
        out.print("     height: 25px;");
        out.print("     line-height: 30px;");
        out.print("     text-align: center;");
        out.print("     border: 1px solid #ccc;");
        out.print(" }");
        out.print(" th {");
        out.print("     background: #eee;");
        out.print("     font-weight: normal;");
        out.print(" }");
        out.print(" tr {");
        out.print("     background: #fff;");
        out.print(" }");
        out.print(" tr:hover {");
        out.print("     background: rgb(255, 255, 173);");
        out.print(" }");
        out.print(" td a {");
        out.print("     color: #06f;");
        out.print("     text-decoration: none;");
        out.print(" }");
        out.print(" td a:hover {");
        out.print("     color: white;");
        out.print(" }");
        out.print(" a {");
        out.print("     display: inline-block;");
        out.print("     margin: 0px 0px 5px 5px;");
        out.print("     padding: 6px 8px;");
        out.print("     font-size: 16px;");
        out.print("     outline: none;");
        out.print("     text-align: center;");
        out.print("     width: 70px;");
        out.print("     line-height: 16px;");
        out.print("     cursor: pointer;");
        out.print(" }");
        out.print(" a:hover {");
        out.print("     color: white;");
        out.print("     background-color: rgba(0, 106, 255, 0.8);");
        out.print(" }");
        out.print("    </style>");

        //<!--负责删除的js函数，发送请求-->
        out.print("<script type='text/javascript'>");
        out.print("    function del(Sno){");
        out.print("        var ok = window.confirm('警告：你正在删除该学生及其对应消费记录！！！');");
        out.print("        if(ok){");
        out.print("            document.location.href = '" + contextPath + "/student/del?Sno=' + Sno;");
        out.print("        }");
        out.print("    }");
        out.print("</script>");

        out.print("</head>");
        out.print("<body>");
        out.print("    <h1>学生表</h1>");
        out.print("    <table align='center' cellpadding='0' cellspacing='0' border='1'>");
        out.print("        <title>学生表</title>");
        out.print("        <tr>");
        out.print("            <th>姓名</th>");
        out.print("            <th>操作</th>");
        out.print("        </tr>");
        //<!--上面是固定的-->

        // 通过数据库获取学生链表
        StudentList list = StudentServer.list();
        // 设置缓冲变量
        Student temp = list.getFirst().getNext();

        // 遍历学生列表
        while (temp != null) {
            out.print("        <tr>");
            out.print("            <td>" + temp.getName() + "</td>");
            // 将对应学生的学号放入链接，形成动态链接
            out.print("            <td><a href='" + contextPath + "/fare/list?Sno=" + temp.getNo() + "' >支出</a>");
            out.print("                <a href='javascript:void(0)' onclick='del(" + temp.getNo() + ")' >删除</a>");
            out.print("                <a href='" + contextPath + "/student/update?Sno=" + temp.getNo() + "' >修改</a></td>");
            out.print("        </tr>");
            temp = temp.getNext();
        }

        // <!--下面是固定的-->");
        out.print("    </table>");
        out.print("    <div>");
        out.print("        <a href='" + contextPath + "/Student_Create.html'>添加学生</a>");
        out.print("    </div>");
        out.print("</body>");
        out.print("</html>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 该方法处理post请求，其调用get请求，欺骗小猫咪
        doGet(request,response);
    }
}
